import businessCardApi from '../../services/businessCard/businessCard';
import focusApi from '../../services/focus/focus';
import orderApi from '../../services/order/order';
import commentApi from '../../services/comment/comment';
import dateUtil from '../../services/_utils/date';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    comments:[],
    teacher: {
      userId: null,
      id: null,
      teacherName: '',
      title: '',
      company: '',
      position: '',
      icon: null,
      industry: '',
      phone: '',
      wx: '',
      skills: '',
      address: '',
      contents: [{
          type: 'txt',
          content: ''
        },
        {
          type: 'img',
          content: ''
        },
      ]
    }

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    businessCardApi.getDetailById(options.id).then(res => {
      console.log(res)
      this.setData({
        teacher: res
      })
      commentApi.getComments(res.userId).then(res => {
        console.log(res)
        res.forEach(o => {
          o.createTime = dateUtil.formatDateTime(o.createTime)
        })
        
        this.setData({
          comments: res
        })
      })
    })
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  focus() {
    focusApi.focus(this.data.teacher.userId).then(res => {
      wx.showToast({
        title: '成功',
        icon: 'success',
        duration: 1500
      })
    })
  },

  toPayAndChat() {
    // --todo  此处需要完善，需要先支付和获取单号
    let data = {
      orderId: 'D125002',
      userId: getApp().globalData.userInfo.userId,
      teacherId: this.data.teacher.userId,
      price: this.data.teacher.price
    }
    orderApi.create(data).then(res => {
      console.log("create", res)
      if (res.code == 200) {
        wx.navigateTo({
          url: '/pages/chat/chatRoom/chatRoom?groupId=' + res.groupId + '&userId=' + res.toUserId + '&toUserIcon=' + res.toUserIcon
        });
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'error',
          duration: 2000
        })
      }
    })

  }
})